/*
 * AttributeDefinition.java
 *
 * Created on 14 de diciembre de 2004, 16:48
 */

package ocp.context;

/**
 * <p>ContextClass attributes represents attributes from contextual information objects, such as
 * the name of the room the user is in, in a Physical Location Contextual Information.</p>
 *
 * An example of AttributeDefinition creation could be:<br>
 * AttributeDefinition intAtt=new AttributeDefinition("IntAttribute",Class.forName("java.lang.Integer"));
 *
 * @author  nacho
 */
public class AttributeDefinition implements java.io.Serializable {
    
    protected String name;
    protected Class type;
    
    /** Creates a new instance of AttributeDefinition. The type is set to the name contained in the 'type' param.
     * If no class is associated with that class name, object type is set to 'Object'.
     * @param name the name of the attribute.
     * @param type the name of the attribute type (i.e: java.lang.String).
     */
    public AttributeDefinition(String name, String type) {
        this.name = name;
        try { 
            this.type = Class.forName(type);
        } catch (ClassNotFoundException cnfe) { this.type = Object.class; }

    }
    
    /** Creates a new instance of AttributeDefinition. type is used as the type of the attribute */
    public AttributeDefinition(String name, Class type) {
        this.name = name;
        this.type = type;
    }
        
    /** returns the name of the attribute */
    public String getName()     { return name; }
    
    /** returns the string representation of the type of the attribute */
    public String getTypeName() { return type.getName(); }
    
    /** returns the Class representing the tyoe of the attribute */
    public Class getType()      { return type; }
    
    /** returns true if passed object is equal to this attribute, false otherwise */
    public boolean equals(Object o) {
        if (o instanceof AttributeDefinition) {
            AttributeDefinition ad = (AttributeDefinition) o;
            if (ad.getName().equals(this.name) && ad.getType().equals(this.type))
                return true;
        } 
        return false;
    }
    
    public int hashCode() { return name.hashCode() + type.hashCode(); }
    
    public String toXML() { 
        return "<AttributeDefinition><Name>"+name+"</Name><Type>"+type.getName()+"</Type></AttributeDefinition>\n";
    }
}
